# -*- coding: utf-8 -*-
'''
Created on Apr 3, 2012

@author: LONG HOANG GIANG
'''

import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import simplejson as json
import gzip
import datetime
from CrawlerLib.http import Http

FILE_DATA = 'output.gz'

def loadData(fileName):
    f = gzip.open(fileName, 'rb')
#    f = open(fileName, 'rb')
    content = f.read()
    f.close()
    data = json.loads(content)
    return data;

def saveData(fileName, data):
    content = json.dumps(data)
    f = gzip.open(fileName, 'wb')
    f.write(content)
    f.close()

def pushFirst(item):
    data = loadData(FILE_DATA)
    n = len(data)
    data.append(None)
    for i in range(n, 0, -1):
        data[i] = data[i-1]
    data[0] = item;
    saveData(FILE_DATA, data)

def pushLast(item):
    data = loadData(FILE_DATA)
    data.append(item)
    saveData(FILE_DATA, data)

def getImagesOfChapter(url, chapterName):
    
    tree = Http.getXMLTree(url)
    xpathNode = "//div[@class='entry']//img"
    for item in tree.xpath(xpathNode):
        print item.get('src')
    
def update(chapterName):
    
    images = '''http://4.bp.blogspot.com/-rGs6IwPUgVM/T4DdhGDlV3I/AAAAAAAA-JM/wLcHXrnHXKg/s1200/dragon-ball-001.jpg
http://4.bp.blogspot.com/-piabyivROcw/T4Ddh7D1M6I/AAAAAAAA-JU/TUMHecsdB48/s1200/dragon-ball-002.jpg
http://4.bp.blogspot.com/-u6S9f8JcMoM/T4Ddhig3hNI/AAAAAAAA-JQ/o3GmcP7UWjs/s1200/dragon-ball-003.jpg
http://3.bp.blogspot.com/-plvVejRs5xQ/T4Ddi0Lfk2I/AAAAAAAA-Jg/iYkcfAvFzvA/s1200/dragon-ball-004.jpg
http://4.bp.blogspot.com/-q2p_1N_P9Iw/T4Ddj1FA4yI/AAAAAAAA-Jo/AaC8zULoy0Q/s1200/dragon-ball-005.jpg
http://4.bp.blogspot.com/-T8q5-RF_4wA/T4Ddm79a_dI/AAAAAAAA-KA/YJsB46OA8-0/s1200/dragon-ball-006.jpg
http://4.bp.blogspot.com/-FLi4UKcZnYs/T4Ddk_zcMpI/AAAAAAAA-Jw/ZnJO8_XKbl0/s1200/dragon-ball-007.jpg
http://3.bp.blogspot.com/-C_ROOv7HV4Q/T4DdmWcxxqI/AAAAAAAA-J8/FYrVTt-6xWY/s1200/dragon-ball-008.jpg
http://4.bp.blogspot.com/-Sy4NfEnopDc/T4Ddm-xB3aI/AAAAAAAA-KE/UKvQXCILEUs/s1200/dragon-ball-009.jpg
http://4.bp.blogspot.com/-VOEOdD-l3iA/T4DdoZUweLI/AAAAAAAA-KU/ZC2dkEcMaaU/s1200/dragon-ball-010.jpg
http://4.bp.blogspot.com/-pbJilG4pc3c/T4DdpOoAnFI/AAAAAAAA-KY/iCiqbUJOcdg/s1200/dragon-ball-011.jpg
http://4.bp.blogspot.com/-Pu1n35LOeu0/T4Ddphk6R6I/AAAAAAAA-Kg/5w_0V4cXY6o/s1200/dragon-ball-012.jpg
http://4.bp.blogspot.com/-bTvReN_oljw/T4DdqKnY-uI/AAAAAAAA-Kk/b7XX-3DLAQk/s1200/dragon-ball-013.jpg
http://3.bp.blogspot.com/-sPxdi5A4szU/T4Ddq0jvieI/AAAAAAAA-Ks/ln0q_cwbvlk/s1200/dragon-ball-014.jpg
http://3.bp.blogspot.com/-V23thoFeb8I/T4DdrVUtxzI/AAAAAAAA-Kw/bcLz7G55CoY/s1200/dragon-ball-015.jpg'''
    
    images = images.strip().split("\n")
    item = {'chapter': chapterName, 'images': images}
    pushFirst(item)
        
if __name__ == '__main__':
    
#    getImagesOfChapter('http://doctruyen.vechai.info/dragon-ball-chap-170/', 'Dragon Ball 170')
    update('Dragon Ball 170')
    
    print('>>Finished at {0}'.format(datetime.datetime.now()))
    os._exit(1)

    